package com.xizi.lotterySystem.utils;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class EmailSenderUtil {

    public static void sendEmail(String toEmail, String verificationCode) {
        String host = "smtp.qq.com";
        final String username = "3218615419@qq.com";
        final String password = "whflkatiwaoxdcgf";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
            message.setSubject("登入验证码");
            message.setText("您的验证码是: " + verificationCode + "");

            Transport.send(message);
            System.out.println("Sent message successfully...." + "code:" + verificationCode);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

